package wxy;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class LeetCode816 {
    public List<String> ambiguousCoordinates(String s) {
        List<String> resultList = new ArrayList<>();
        String substring = s.substring(1, s.length() - 1);
        char[] digits = substring.toCharArray();
        for (int i = 1; i < digits.length; i++) {
            List<String> left = toNum(Arrays.copyOfRange(digits, 0, i));
            List<String> right = toNum(Arrays.copyOfRange(digits, i, digits.length));
            if (left.size() > 0 && right.size() > 0) {
                for (String s1 : left) {
                    for (String s2 : right) {
                        resultList.add("(" + s1 + ", " + s2 + ")");
                    }
                }
            }
        }
        return resultList;
    }

    private List<String> toNum(char[] digits) {
        List<String> list = new ArrayList<>();
        if (digits.length == 1) {
            list.add(digits[0] + "");
        } else {
            if (digits[0] == '0' && digits[digits.length - 1] != '0') {
                StringBuilder sb = new StringBuilder();
                sb.append('0').append('.');
                for (int i = 1; i < digits.length; i++) {
                    sb.append(digits[i]);
                }
                list.add(sb.toString());
            } else if (digits[0] != '0' && digits[digits.length - 1] == '0') {
                list.add(new String(digits));
            } else if ((digits[0] != '0' && digits[digits.length - 1] != '0')) {
                for (int i = 0; i < digits.length; i++) {
                    StringBuilder sb = new StringBuilder();
                    for (int j = 0; j < digits.length; j++) {
                        sb.append(digits[j]);
                        if (j == i && j != digits.length - 1) {
                            sb.append(".");
                        }
                    }
                    list.add(sb.toString());
                }
            }
        }
        return list;
    }
}
